package com.api.demo.mapper;

import com.api.demo.entity.ContentLike;
import com.api.demo.entity.User;
import com.api.demo.entity.UserFollow;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserFollowMapper extends BaseMapper<UserFollow> {

        @Select("SELECT COUNT(*) FROM user_follow WHERE follower_id = #{userId}")
        int countFollowing(@Param("userId") Long userId);

        @Select("SELECT COUNT(*) FROM user_follow WHERE following_id = #{userId}")
        int countFollowers(@Param("userId") Long userId);

        @Select("SELECT u.* FROM user u JOIN user_follow uf ON u.id = uf.following_id WHERE uf.follower_id = #{userId}")
        List<com.api.demo.entity.User> findFollowingUsers(@Param("userId") Long userId);

        @Select("SELECT u.* FROM user u JOIN user_follow uf ON u.id = uf.follower_id WHERE uf.following_id = #{userId}")
        List<com.api.demo.entity.User> findFollowers(@Param("userId") Long userId);

        @Select("SELECT u.* FROM user u JOIN user_follow uf ON u.id = uf.follower_id WHERE uf.following_id = #{userId} LIMIT #{offset}, #{limit}")
        List<com.api.demo.entity.User> findFollowersWithPagination(@Param("userId") Long userId,
                @Param("offset") int offset, @Param("limit") int limit);

        @Select("SELECT * FROM user_follow " +
                "WHERE follower_id = #{followerId} " +
                "AND following_id = #{followingId} " +
                "LIMIT 1")
        UserFollow findFollowRelationship(
                @Param("followerId") Long followerId,
                @Param("followingId") Long followingId);

                        // 在UserFollowMapper接口中添加
        @Select("SELECT u.* FROM user u " +
                "INNER JOIN user_follow uf ON u.id = uf.following_id " +
                "WHERE uf.follower_id = #{userId} " +
                "ORDER BY uf.create_time DESC " +
                "LIMIT #{limit} OFFSET #{offset}")
        List<User> findFollowingWithPagination(@Param("userId") Long userId, 
                                        @Param("offset") int offset, 
                                        @Param("limit") int limit);
        }

